package com.admin.controller;

import com.admin.util.CommonUtil;
import com.admin.util.Result;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.region.Region;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;


@RestController
@RequestMapping(value = "/tengxun")
@ApiIgnore
/**
 * @ignore true
 */
public class UploadTXController {
    private String secretId="AKIDUjgdPPH86sP3TdbL2OhjR3WnyKG2Knpm";     //腾讯云对象存储的secretId
    private String secretKey="6NLuUM0gMEyIPG1tlSEvc6KJBqmmj4II";        //腾讯云对象存储的secretKey
    private String bucket="ap-chengdu";                                 //所属地域
    private String bucketName="wgh0315-1303990880";                     //存储桶名称
    private String path="https://wgh0315-1303990880.coscd.myqcloud.com";//访问域名
    private String qianzhui="wgh0315";      //上传目录


    @PostMapping("/upload")
    @ResponseBody
    public Object Upload(@RequestParam(value = "file") MultipartFile file, HttpSession session) {
        if (CommonUtil.isEmpty(file)) {
            return new Result(Result.Code.FAILED,"文件不能为空！");
        }
        String oldFileName = file.getOriginalFilename();
        String eName = oldFileName.substring(oldFileName.lastIndexOf(".")); //获取文件后缀
        String newFileName = UUID.randomUUID() + eName; //文件名
        // 1 初始化用户身份信息(secretId, secretKey)
        COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
        // ueditor 设置bucket的区域, COS地域的简称请参照 https://cloud.tencent.com/document/product/436/6224
        ClientConfig clientConfig = new ClientConfig(new Region(bucket));
        // 3 生成cos客户端
        COSClient cosclient = new COSClient(cred, clientConfig);
        // bucket的命名规则为{name}-{appid} ,此处填写的存储桶名称必须为此格式
        String bucketName = this.bucketName;

        // 简单文件上传, 最大支持 5 GB, 适用于小文件上传, 建议 20 M 以下的文件使用该接口
        // 大文件上传请参照 API 文档高级 API 上传
        File localFile = null;
        try {
            localFile = File.createTempFile("temp", null);
            file.transferTo(localFile);
            // 指定要上传到 COS 上的路径
            String key = "/" + this.qianzhui + "/"  + newFileName;
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, key, localFile);
            PutObjectResult putObjectResult = cosclient.putObject(putObjectRequest);
            Map<String, Object> data = new HashMap<>();
            data.put("url",this.path + putObjectRequest.getKey());
            data.put("code", 200);
            data.put("msg", "成功");
            data.put("size", file.getSize());
            return data;
        } catch (IOException e) {
            return new Result(Result.Code.FAILED,e.getMessage());
        } finally {
            // 关闭客户端(关闭后台线程)
            cosclient.shutdown();
        }
    }


}
